

Function LineCool(x0#, y0#, x1#, y1#, width#)
'slope
Local dx# = x0 - x1
Local dy# = y0 - y1


Local verts:Float[] ':Float[36]

Local Mag:Double = Sqr(dx*dx + dy*dy)
dx = dx / mag * width
dy = dy / mag * width

		
		Local endpoints:Int = 1	
		local rot:int = 0
		If EndPoints
		
			verts = New Float[72]
			Local c:Int Ptr=Int Ptr(Float Ptr(verts))
	
			verts[0]	=	x0 - dy + dx
			verts[1] 	=	y0 + dx + dy
			verts[2]	=	0
			verts[3]	=	0 ' Normals
			verts[4]	=	0 ' Normals
			verts[5]	=	1 ' Normals
			'c[6]		=	c0
			verts[7]	=	1
			verts[8] 	=	0
			
			verts[9]	=	x0 + dy + dx 
			verts[10] 	=	y0 - dx + dy
			verts[11]	=	0
			verts[12]	=	0 ' Normals
			verts[13]	=	0 ' Normals
			verts[14]	=	1 ' Normals
			'c[15]		=	c0
			verts[16]	=	1
			verts[17] 	=	1
		
			verts[18]	=	x0 - dy
			verts[19] 	=	y0 + dx
			verts[20]	=	0
			verts[21]	=	0 ' Normals
			verts[22]	=	0 ' Normals
			verts[23]	=	1 ' Normals
			'c[24]		=	c0
			verts[25]	=	0.4985
			verts[26] 	=	0
		
			verts[27]	=	x0 + dy
			verts[28]	=	y0 - dx
			verts[29]	=	0
			verts[30]	=	0 ' Normals
			verts[31]	=	0 ' Normals
			verts[32]	=	1 ' Normals
			'c[33]		=	c0
			verts[34]	=	0.4985
			verts[35]	=	1
			
			verts[36]	=	x1 - dy
			verts[37]	=	y1 + dx
			verts[38]	=	0
			verts[39]	=	0 ' Normals
			verts[40]	=	0 ' Normals
			verts[41]	=	1 ' Normals
			'c[42]		=	c1
			verts[43]	=	0.5015
			verts[44]	=	0
			
			verts[45]	=	x1 + dy
			verts[46]	=	y1 - dx
			verts[47]	=	0
			verts[48]	=	0 ' Normals
			verts[49]	=	0 ' Normals
			verts[50]	=	1 ' Normals
			'c[51]		=	c1
			verts[52]	=	0.5015
			verts[53]	=	1
			
			verts[54]	=	x1 - dy - dx
			verts[55]	=	y1 + dx - dy
			verts[56]	=	0
			verts[57]	=	0 ' Normals
			verts[58]	=	0 ' Normals
			verts[59]	=	1 ' Normals
			'c[60]		=	c1
			verts[61]	=	1
			verts[62]	=	0
			
			verts[63]	=	x1 + dy - dx
			verts[64]	=	y1 - dx - dy
			verts[65]	=	0
			verts[66]	=	0 ' Normals
			verts[67]	=	0 ' Normals
			verts[68]	=	1 ' Normals
			'c[69]		=	c1
			verts[70]	=	1
			verts[71]	=	1
			
		Else
			verts = New Float[36]
			'Local c:Int Ptr=Int Ptr(Float Ptr(verts))
			
			
			If rot
				verts[7]	=	0
				verts[8] 	=	0
				
				verts[16]	=	1
				verts[17]	=	0
				
				verts[25]	=	0
				verts[26]	=	1
				
				verts[34]	=	1
				verts[35]	=	1
			
			Else
				verts[7]	=	0
				verts[8] 	=	0
				
				verts[16]	=	0
				verts[17]	=	1
				
				verts[25]	=	1
				verts[26]	=	0
				
				verts[34]	=	1
				verts[35]	=	1
			EndIf
		
			verts[0]	=	x0 - dy
			verts[1] 	=	y0 + dx
			verts[2]	=	0
			verts[3]	=	0 ' Normals
			verts[4]	=	0 ' Normals
			verts[5]	=	1 ' Normals			
			'c[6]		=	c0

			verts[9]	=	x0 + dy
			verts[10]	=	y0 - dx
			verts[11]	=	0
			verts[12]	=	0 ' Normals
			verts[13]	=	0 ' Normals
			verts[14]	=	1 ' Normals
			'c[15]		=	c0
		
			verts[18]	=	x1 - dy
			verts[19]	=	y1 + dx
			verts[20]	=	0
			verts[21]	=	0 ' Normals
			verts[22]	=	0 ' Normals
			verts[23]	=	1 ' Normals
			'c[24]		=	c1
			
			verts[27]	=	x1 + dy
			verts[28]	=	y1 - dx
			verts[29]	=	0
			verts[30]	=	0 ' Normals
			verts[31]	=	0 ' Normals
			verts[32]	=	1 ' Normals
			'c[33]		=	c1

		EndIf
'glPolygonMode(GL_FRONT, GL_LINE)

'DrawTexturedPoly Image, verts



	local particleImg:TGLImageFrame = TGLImageFrame(image.Frame(0))
	local texture_name:Int = particleImg.name

	glBindTexture GL_TEXTURE_2D, texture_name
	glEnable GL_TEXTURE_2D

	
	glBegin GL_TRIANGLE_STRIP
	For Local i=0 Until Len(verts)-1 Step 9
		glNormal3f verts[i+3],verts[i+4],verts[i+5]
		glTexCoord2f verts[i+7],verts[i+8]
		glVertex3f verts[i],verts[i+1],verts[i+2]
	Next
	glEnd

'DrawImage TestImage, 200, 200
End Function

Function DrawTexturedPoly( image:TImage,xyuv#[],frame=0, vertex = -1)
	Local handle_x#,  handle_y#
	GetHandle handle_x#,  handle_y#
	Local origin_x#,  origin_y#
	GetOrigin origin_x#,  origin_y#	
	
	Local D3DDriver:TD3D7Max2DDriver = TD3D7Max2DDriver(_max2dDriver)
	
	Assert Image, "Image not found"
	
	If D3DDriver Then 
		DrawTexturedPolyD3D ..
			D3DDriver,..
			 TD3D7ImageFrame(image.Frame(frame)), ..
			 xyuv, handle_x, handle_y, origin_x,origin_y, vertex*4
		Return
	End If
	Local  OGLDriver:TGLMax2DDriver = TGLMax2DDriver(_max2dDriver)
	If OGLDriver Then
			DrawTexturedPolyOGL ..
				OGLDriver,..
				 TGLImageFrame(image.Frame(frame)), ..
				 xyuv, handle_x, handle_y, origin_x,origin_y,  vertex*4
		Return
	End If
End Function


Function DrawTexturedPolyD3D( Driver:TD3D7Max2DDriver,  Frame:TD3D7ImageFrame,xyuv#[],handlex#,handley#,tx#,ty# , vertex)
	'If Driver.islost Return
	If xyuv.length<6 Return
	Local segs=xyuv.length/4
	Local len_ = Len(xyuv)
	
	If vertex > - 1 Then
		segs = vertex / 4
		len_ = vertex
	End If
	Local uv#[] = New Float[segs*6] ' 6
	

	Local c:Int Ptr=Int Ptr(Float Ptr(uv))
	
	Local ii:Int = 0
	For Local i=0 Until len_ Step 4
		Local x# =  xyuv[i+0]+handlex
		Local y# =  xyuv[i+1]+handley
		uv[ii+0] =  x*Driver.ix+y*Driver.iy+tx
		uv[ii+1] =  x*Driver.jx+y*Driver.jy+ty 
		uv[ii+2] =  0  ' *********** THIS IS THE Z-COORDINATE
		c[ii+3] = Driver.DrawColor
		uv[ii+4] =  xyuv[i+2]
		uv[ii+5] =  xyuv[i+3]
		ii:+6
	Next
	Driver.SetActiveFrame Frame
	Driver.device.DrawPrimitive(D3DPT_TRIANGLEFAN,D3DFVF_XYZ| D3DFVF_DIFFUSE | D3DFVF_TEX1,uv,segs,0)
End Function


Function DrawTexturedPolyOGL (Driver:TGLMax2DDriver, Frame:TGLImageFrame, xy#[],handle_x#,handle_y#,origin_x#,origin_y#, vertex) 
	'Private
	'Global TmpImage:TImage
	'Public
	
	If xy.length<6 Return
	
	rem
	Local rot#  = GetRotation()
	Local tform_scale_x#, tform_scale_y#
	GetScale tform_scale_x, tform_scale_y
	
	Local s#=Sin(rot)
	Local c#=Cos(rot)
	Local ix= c*tform_scale_x
	Local iy=-s*tform_scale_y
	Local jx= s*tform_scale_x
	Local jy= c*tform_scale_y
	end rem
	
	'glcolor3f 1,1,1
	glBindTexture GL_TEXTURE_2D, Frame.name
	glEnable GL_TEXTURE_2D
	'glDisable GL_TEXTURE_2D
	
	glBegin GL_TRIANGLE_STRIP'GL_POLYGON
	'For Local i=0 Until Len xy Step 4
	For Local i=0 Until Len(xy)-1 Step 9
		glNormal3f xy[i+3],xy[i+4],xy[i+5]
		glTexCoord2f xy[i+7],xy[i+8]
		glVertex3f xy[i],xy[i+1],xy[i+2]
	
		'If vertex > -1 And i >= vertex Then Exit
		'Local x#=xy[i+0]+handle_x
		'Local y#=xy[i+1]+handle_y
		'Local u#=xy[i+2]
		'Local v#=xy[i+3]
		'glTexCoord2f u,v
		'glVertex2f x*ix+y*iy+origin_x,x*jx+y*jy+origin_y
	Next
	glEnd
	'If Not tmpImage Then tmpImage = CreateImage(1,1)
	'DrawImage tmpImage, -100, - 100 ' Chtob zbit' flag texturi
End Function

